package com.example.bill.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.bill.R;
import com.example.bill.application.BillApplication;
import com.example.bill.entity.Memo;
import com.example.bill.repository.MemoRepository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * A fragment representing a list of Items.
 */
public class BillListFragment extends Fragment {
    private static final String TAG = "BillListFragment";

    // TODO: Customize parameter argument names
    private static final String YEAR = "year";
    public static final String MONTH = "Month";
    // TODO: Customize parameters
    private int year = 2024;
    private int month = 2024;
    private MemoRepository dataBase;
    private SimpleAdapter adapter;
    private final List<HashMap<String, Object>> data = new ArrayList<>();

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public BillListFragment() {
    }

    // TODO: Customize parameter initialization
    @SuppressWarnings("unused")
    public static BillListFragment newInstance(int year, int month) {
        BillListFragment fragment = new BillListFragment();
        Bundle args = new Bundle();
        args.putInt(YEAR, year);
        args.putInt(MONTH, month);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            year = getArguments().getInt(YEAR);
            month = getArguments().getInt(MONTH);
        }

        dataBase = BillApplication.getBillApplication().getDataBase();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ListView view = (ListView) inflater.inflate(R.layout.fragment_memo_list, container, false);

        refresh();

        adapter = new SimpleAdapter(getContext(), data, R.layout.fragment_memo, new String[]{
                "date",
                "name",
                "type"
        }, new int[]{
                R.id.date,
                R.id.name,
                R.id.type
        });

        view.setAdapter(adapter);

        return view;
    }

    private void refresh() {
        // Set the adapter
        List<Memo> memos = dataBase.queryByDate(year, month);

        data.clear();

        memos.stream().map(memo -> {
            HashMap<String, Object> r = new HashMap<>();

            String date = SimpleDateFormat.getDateInstance().format(memo.getDate());

            r.put("date", date);
            r.put("name", memo.getName());
            r.put("type", (memo.getType() == 1 ? "+" : "-") + memo.getAmount());
            return r;
        }).forEach(data::add);


    }

    @Override
    public void onStart() {
        super.onStart();
        Log.d(TAG, "onStart " + month + "月");

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d(TAG, "onResume " + month + "月");
        refresh();
        adapter.notifyDataSetChanged();
    }
}